import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;


public class SettingsFrame extends JFrame {
	
	private final int FRAME_WIDTH = 475;
	private final int FRAME_HEIGHT = 300;
	
	private JTextField exportLoc;
	private JTable scanLoc;
	private DefaultTableModel model;
	private JCheckBox scanOnStart;
	private JButton btnSubmit, btnApply, btnCancel, btnExportBrowse, btnAdd, btnRemove;
	private JLabel lblExport, lblScan;
	private JPanel btnPanel, scanPanel, exportPanel, mainPanel, arPanel;
	private JFileChooser fc;
	
	public SettingsFrame()
	{
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		setLayout(new GridLayout(1,1));
		fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		createTable();
		createButtons();
		createInputs();
		
		createCheckBoxes();
		createLabels();
		createPanels();
	}

	private void createTable()
	{
		model = new DefaultTableModel(){

		    @Override
		    public boolean isCellEditable(int row, int column) {
		       return false;
		    }
		};
		model.addColumn("Scan Locations");
		scanLoc = new JTable(model);
		scanLoc.setToolTipText("You may enter more than one location");
		scanLoc.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		scanLoc.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		scanLoc.setPreferredSize(new Dimension(100, 75));
		
	}
	
	private void createButtons()
	{
		btnSubmit = new JButton("Submit");
		btnCancel = new JButton("Cancel");
		btnApply = new JButton("Apply");
		btnExportBrowse = new JButton("Browse...");
		btnAdd = new JButton("+");
		btnRemove = new JButton("-");
		
		class SubmitListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				changeFilePath();
				setVisible(false);
			}
		}
		class ApplyListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				changeFilePath();
			}
		}
		class CancelListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		}
		class ExportListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				fc.showOpenDialog(null);
				exportLoc.setText(fc.getSelectedFile().getPath());
			}
		}
		class AddListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				fc.showOpenDialog(null);
				model.addRow(new Object[]{fc.getSelectedFile().getPath()});
			}
		}
		class RemoveListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = scanLoc.getSelectedRow();
				if (row != -1)
				{
					model.removeRow(row);
				}
			}
		}
		
		ActionListener a = new AddListener();
		btnAdd.addActionListener(a);
		
		ActionListener r = new RemoveListener();
		btnRemove.addActionListener(r);
		
		ActionListener sub = new SubmitListener();
		btnSubmit.addActionListener(sub);
		
		ActionListener can = new CancelListener();
		btnCancel.addActionListener(can);
		
		ActionListener app = new ApplyListener();
		btnApply.addActionListener(app);
		
		ActionListener ex = new ExportListener();
		btnExportBrowse.addActionListener(ex);
		
		
	}
	
	private void createCheckBoxes()
	{
		scanOnStart = new JCheckBox("Scan folders on program start");
	}
	
	private void createInputs()
	{
		exportLoc = new JTextField();
		exportLoc.setPreferredSize(new Dimension(275, 25));

	}
	
	private void createLabels()
	{
		lblExport = new JLabel("Export to: "); 
		lblScan = new JLabel("Folders to scan: "); 
	}
	
	private void createPanels()
	{
		btnPanel = new JPanel(new FlowLayout());
		exportPanel = new JPanel(new FlowLayout());
		scanPanel = new JPanel(new FlowLayout());
		mainPanel = new JPanel(new GridLayout(3, 3));
		arPanel = new JPanel(new GridLayout(2,1));
		
		exportPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scanPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		arPanel.add(btnAdd);
		arPanel.add(btnRemove);
		
		scanPanel.add(lblScan);
		scanPanel.add(scanLoc);
		scanPanel.add(arPanel);
		scanPanel.add(scanOnStart);
		
		exportPanel.add(lblExport);
		exportPanel.add(exportLoc);
		exportPanel.add(btnExportBrowse);
		
		btnPanel.add(btnSubmit);
		btnPanel.add(btnCancel);
		btnPanel.add(btnApply);

		mainPanel.add(exportPanel);
		mainPanel.add(scanPanel);
		mainPanel.add(btnPanel);
		
		add(mainPanel);
	}
	
	private void changeFilePath()
	{
		int rows = model.getRowCount();
		String[] fp = new String[rows];
		
		for(int i = 0; i < rows; i++)
		{
			fp[i] = model.getValueAt(i, 0).toString().replace("\\", "\\\\");
		}
		MainFrame.filmz.setExportPath(exportLoc.getText());
		MainFrame.filmz.setFilePath(fp);
		MainFrame.filmz.setFilePathSet(true);
	}
}
